(function (window) {
	'use strict';

	 //1.创建模块
	var app = angular.module('todoApp', ['service']);

	//2.创建控制器
	app.controller('todoController', ['$scope', '$location', 'myService', function($scope, $location, myService){
		//新输入的任务
		$scope.newTodo = '';

		$scope.data = myService.getData();

		//1.功能在列表中展示所有的任务，在页面中使用ng-repeat

		//2.在列表中添加任务
		$scope.add = function(){
			myService.add($scope.data, $scope.newTodo);
			$scope.newTodo = '';
		}

		//3.点击任务后面的关闭按钮
		$scope.delete = function(id){
			myService.delete($scope.data, id);
		}

		//4.批量切换任务状态
		$scope.selectAll = false;
		$scope.toggleAll = function(){
			myService.toggleAll($scope.data, $scope.selectAll);
		}


		//5.修改任务内容
		$scope.isEditing = -1;
		$scope.edit = function(id){
			$scope.isEditing = id;
		}

		$scope.save = function(){
			myService.setData($scope.data);
			$scope.isEditing = -1;
		}

		//6.显示未完成任务个数
		$scope.getActive = function(){
			return myService.getActive($scope.data);
		}

		//7.清除所有已完成任务
		$scope.removeAll = function(){
			myService.removeAll($scope.data);
		}

		//8.完成不同状态任务切换
		$scope.loc = $location;
		$scope.$watch('loc.url()', function(now, old){
			switch(now){
				case '/active':
					$scope.isCompleted = {completed:false};
					break;
				case '/completed':
					$scope.isCompleted = {completed:true};
					break;
				default:
					$scope.isCompleted = {};
			}
		})
	}]);
})(window);
